package com.fjrz.bbxwj.main.host.adpter

import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.base.loadRoundImage
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.VideoDraft

class VideoDraftAdapter : BaseQuickAdapter<VideoDraft, BaseViewHolder>(R.layout.item_sketch_box) {

    var isAction = false
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }


    val selectCountLiveData = MutableLiveData<Int>()

    override fun convert(holder: BaseViewHolder, item: VideoDraft) {
        holder.setText(R.id.tvDraftBoxTitle, item.title)
            .setText(R.id.tvDraftBoxDesc, item.description)
            .getView<ImageView>(R.id.ivDraftBoxImage)
            .loadRoundImage(item.main_image, ConvertUtils.dp2px((10f)))

        val cbDraftBoxSelect: CheckBox = holder.getView<CheckBox>(R.id.cbDraftBoxSelect)
            .apply { isClickable = false }.apply { isChecked = item.isChecked }
            .apply { visibility = if (isAction) View.VISIBLE else View.GONE }

    }

    override fun setOnItemClick(v: View, position: Int) {
        if (!isAction) {
            super.setOnItemClick(v, position)
        } else {
            setData(position, data[position].apply { isChecked = !isChecked })
            selectCountLiveData.postValue(videoSelectCount())
        }
    }

    private fun videoSelectCount(): Int {
        return data.filter { it.isChecked }.size
    }

    fun videoSelectList(): MutableList<VideoDraft> {
        return data.filter { it.isChecked }.toMutableList()
    }


}